import os

SetOption('max_drift', 1)
SetOption('implicit_cache', 1)

opts = Options(['options.cache'], ARGUMENTS)
opts.AddOptions( 
	EnumOption('build', 'build variant', 'Debug', allowed_values=('Debug','Release')),
	PathOption('Ei4SDK', 'Ei4 SDK path', '~/Ei4SDK'),
)

compile_flags = ' -Wall -mthreads -fmessage-length=0 -fexceptions -fident '
debug_flags = ' -g -O0 '
release_flags = ' -O3 -s '

if os.name == 'nt':
   tools=['mingw']
   toolpath=['ei4/WINstaller']
else:
   tools=['default']
   toolpath=[]
 	    
env = Environment( 
		tools=tools,
		toolpath = toolpath,
		options = opts, 
		ENV=os.environ,
		CCFLAGS = compile_flags,
		CPPPATH = ['$Ei4SDK/include', '$Ei4SDK/include/boost-1_33_1'],
		CPPDEFINES = Split('''WIN32 _WINDOWS'''),
		LIBPATH = ['$Ei4SDK/lib'],
		# -Wl,--enable-auto-import suppresses "Info: resolving ... by linking to ... " messages
		# (XXX is this windows specific or what?)
		LINKFLAGS =
		['-Wl,--enable-auto-import','-Wl,--enable-auto-image-base','-Wl,--add-stdcall-alias'],
	)
Help(opts.GenerateHelpText(env))
opts.Save('options.cache', env)

if env['build'] == 'Debug':
	env.Append(
			CCFLAGS = debug_flags,
			CXXFLAGS = debug_flags,
			CPPDEFINES = ['_DEBUG'],
		)
else:
	env.Append(
			CCFLAGS = release_flags,
			CXXFLAGS = release_flags,
			CPPDEFINES = ['NDEBUG'],
		)

#define a new builders for copying files to staged directories
Copy = env.Builder(action = Copy('$TARGET', '$SOURCE'))
env.Append(BUILDERS = {'CopyFile': Copy})



#define a fucntion that add's A buildsuffix to a basename when
# build is Debug and tur in in a action Object
def GetBuildName(basename = '', debugsuffix = '_d', spaced = 'false'):
	if env['build'] == 'Debug':
		name = basename + debugsuffix
	else:
		name = basename
	if spaced  == 'true':
		name = ' ' + name + ' '
        return name
        
# Subprojects that include/link against ogre use ogreEnv
#
# On Win2k we can't have DirectX 9.0 SDK and therefore can't
# use debug version of ogre. It's important to define NDEBUG 
# then so that we include default (not debug) memory manager.
#
# this should change if/when we upgrade to WinXP
ogreEnv = env.Copy()
while ogreEnv['CPPDEFINES'].count('_DEBUG') > 0:
	ogreEnv['CPPDEFINES'].remove('_DEBUG')
ogreEnv.Append(
		CPPPATH = ['$Ei4SDK/include/Ogre'],
		CPPDEFINES = ['NDEBUG'],
	)
	
	

ogreEnv.Append(
		CPPDEFINES = ['HAVE_EMA_SDK'],
		CPPPATH = Split('''
			.
			oSceneLoader
			$Ei4SDK/src/tinyxml
			$Ei4SDK/src/luabind
			$Ei4SDK/include/WOsc
			$Ei4SDK/include/CEGUI
		'''),
		LIBS = Split('''
			OgreMain
			CEGUIBase
			OgreGUIRenderer
			CEGUITaharezLook
			CEGUIWindowsLook
			wosclib
			ws2_32'''
			+ GetBuildName(basename = 'tinyxml', spaced='true')
			+ GetBuildName(basename = 'luabind', spaced='true')
			+ 'lua5.1')
	)


FileBuild = ogreEnv.Program(
		target = GetBuildName(basename = 'TextureTest'), 
		source = Split('''
			App.cpp
			Gui.cpp	
			GuiEvents.cpp	
			TextureTest.cpp	
			Scripting.cpp
			DotScene.cpp
			NetworkInterface.cpp
			NetworkMethods.cpp
			NetManager.cpp
			oSceneLoader/OgreOSMScene.cpp			
		'''),
	)

# Copy FileBuild  to the right location
# All files are staged in the SDK/lib
#staged_exe = env.CopyFile( 
#		target = '$Ei4SDK/lib/' + str(FileBuild[0]),
#		source =  str(FileBuild[0])
#	)

# By default we want only the executable
Default(FileBuild)
